/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.openbravo.pos.sales.shared;

import cn.ekuma.swing.dialog.AbstractKeyBoardJDialog;

import com.openbravo.bean.sales.SharedTicketInfo;
import com.openbravo.pos.base.AppLocal;

import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;

public class JTicketsBagKeyBoardSharedList extends AbstractKeyBoardJDialog {

    private String m_sDialogTicket;
    private List<SharedTicketInfo> tickets;
    private int selectIndex;
    private javax.swing.AbstractListModel model;

    /**
     * Creates new form JTicketsBagSharedList
     */
    private JTicketsBagKeyBoardSharedList(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
    }

    /**
     * Creates new form JTicketsBagSharedList
     */
    private JTicketsBagKeyBoardSharedList(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
    }

    public String showTicketsList(java.util.List<SharedTicketInfo> atickets) {
        m_sDialogTicket = null;
        if (atickets == null || atickets.size() == 0)
            return m_sDialogTicket;
        tickets = atickets;
        model = new AbstractListModel() {
            @Override
            public int getSize() {
                return tickets.size();
            }

            @Override
            public Object getElementAt(int index) {
                return tickets.get(index).getName();
            }
        };
        selectIndex = 0;
        m_jtickets.setModel(model);
        m_jtickets.setSelectedIndex(selectIndex);
        setVisible(true);
        return m_sDialogTicket;
    }

    public static JTicketsBagKeyBoardSharedList newJDialog(JTicketsBagKeyBoardShared ticketsbagshared) {
        Window window = getWindow(ticketsbagshared);
        JTicketsBagKeyBoardSharedList mydialog;
        if (window instanceof Frame) {
            mydialog = new JTicketsBagKeyBoardSharedList((Frame) window, true);
        } else {
            mydialog = new JTicketsBagKeyBoardSharedList((Dialog) window, true);
        }

        mydialog.initComponents();

        mydialog.jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension(35, 35));

        return mydialog;
    }


    @Override
    protected void enterKeyProc() {
        m_sDialogTicket = tickets.get(m_jtickets.getSelectedIndex()).getId();
        dispose();
    }

    @Override
    protected void escapeKeyProc() {
        dispose();
    }

    @Override
    protected void downKeyProc() {
        if (tickets.size() <= 0)
            return;
        selectIndex = m_jtickets.getSelectedIndex();
        if (selectIndex < tickets.size() - 1) {
            selectIndex++;
        } else {
            selectIndex = 0;
        }
        m_jtickets.setSelectedIndex(selectIndex);
    }

    @Override
    protected void upKeyProc() {
        if (tickets.size() <= 0)
            return;
        selectIndex = m_jtickets.getSelectedIndex();
        if (selectIndex > 0) {
            selectIndex--;
        } else {
            selectIndex = tickets.size() - 1;
        }
        m_jtickets.setSelectedIndex(selectIndex);
    }

/**
 private class JButtonTicket extends JButton {

 private SharedTicketInfo m_Ticket;

 public JButtonTicket(SharedTicketInfo ticket){

 super();

 m_Ticket = ticket;
 setFocusPainted(false);
 setFocusable(false);
 setRequestFocusEnabled(false);
 setMargin(new Insets(8, 14, 8, 14));
 setFont(new java.awt.Font ("Dialog", 1, 24));
 //setBackground(new java.awt.Color (220, 220, 220));
 addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent evt) {

 // Selecciono el ticket
 m_sDialogTicket = m_Ticket.getId();

 // y oculto la ventana
 JTicketsBagKeyBoardSharedList.this.setVisible(false);
 }
 });

 setText(ticket.getName());
 }

 public SharedTicketInfo getTicket(){
 return m_Ticket;
 }
 }
 */
    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        jPanel2 = new javax.swing.JPanel();
        m_jtickets = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        m_jButtonCancel = new javax.swing.JButton();

        setTitle(AppLocal.getIntString("caption.tickets")); // NOI18N
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout());

        m_jtickets.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        m_jtickets.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        m_jtickets.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2)
                    enterKeyProc();
            }
        });
        m_jtickets.setCellRenderer(new MyCellRenderer());
        jPanel2.add(m_jtickets, java.awt.BorderLayout.NORTH);

        jScrollPane1.setViewportView(jPanel2);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
        jPanel3.add(jPanel4);

        m_jButtonCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png"))); // NOI18N
        m_jButtonCancel.setText(AppLocal.getIntString("Button.Close")); // NOI18N
        m_jButtonCancel.setFocusPainted(false);
        m_jButtonCancel.setFocusable(false);
        m_jButtonCancel.setMargin(new java.awt.Insets(8, 16, 8, 16));
        m_jButtonCancel.setRequestFocusEnabled(false);
        m_jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_jButtonCancelActionPerformed(evt);
            }
        });
        jPanel3.add(m_jButtonCancel);

        getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width - 340) / 2, (screenSize.height - 272) / 2, 340, 272);
    }// </editor-fold>

    private void m_jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {

        dispose();

    }

    class MyCellRenderer extends JLabel implements ListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            setText(value.toString());
            setFont(new java.awt.Font("Dialog", 1, 24));
            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
            setEnabled(list.isEnabled());
            setOpaque(true);
            return this;
        }
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList m_jtickets;
    private javax.swing.JButton m_jButtonCancel;
    // End of variables declaration

}
